<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%String path = request.getContextPath() ;%>
<%
	String filePath = request.getParameter("filePath");
	if(filePath==null){
		filePath="";
	}
	String countLimit = request.getParameter("countLimit");
	if(countLimit==null){
		countLimit="10";
	}
%>
<script type="text/javascript" src="<%=path %>/resource/lib/plugins/ajaxfileupload.js"></script>
<span class="sq-button-hasicon onlyicon" id="add_upfileBox2"><i class="sqBtnIco_26"></i></span>
<div class="upfileBoxs"></div>
<span class="sq-button sq-button-hasicon" id="btnUpload2"><i class="sqBtnIco_9"></i>上传</span>
<input type="hidden" id="filePath" value="<%=filePath %>"/>
<input type="hidden" id="countLimit" value="<%=countLimit %>"/>
<script type="text/javascript">

	var count = 1;
	var upload_result_params = [];
	var serverPrefixPath = "";

	$(function(){
		$("#add_upfileBox2").click();

		//获取服务器路径前缀
		$.ajax({
			type: 'get',
			url: '<%=path%>/system/sysulfilesrec/getServerPath.do',
			success:function(msg){
				serverPrefixPath = msg;
				console.log(serverPrefixPath);
			}
		});
	});

	$("#btnUpload2").click(function() {
		ajaxFileUploadImg();
	});
	var FileCount=1;
	var _countLimit = $("#countLimit").val();
	$("#add_upfileBox2").click(function(event) {
		if(FileCount>_countLimit){
			$.ligerDialog.SQalert({content: "超出最大上传数量" , SQ_type: "warning" });
			return;
		}
		$Html=CreatUploadInputBox({
			id:"uploads"+FileCount,  //id不跟着序号走！
			clearLine: true,
			deletable: true,
			onDelete: function  (upfileBox) {
				FileCount--;
				setUpfileCount (); //删除的时候重新设置序号
				var delFileName = upfileBox.find('[type=file]')[0].files[0].name;
				var temp;
				for(temp = 0; temp < upload_result_params.length;temp++){
					if(upload_result_params[temp].fileNm == delFileName){
						upload_result_params.splice(temp,1);
						break;
					}
				}
			},
			onChange: function (e) {
				$(e.target).remove_afterUploadIcon();//删除时,移除已上传文件的下载按钮
			}
		});
		$Html.prepend('<span class="UpfileCount"></span>');
		$(".upfileBoxs").append($Html);
		//$Html.insertBefore($(this));
		//$Html.appendTo($("#more"));
		setUpfileCount (); //新增组件后，设置序号
		FileCount++;
	});
	function setUpfileCount () { //设置序号
		$(".UpfileCount").each(function(index, el) {
			$(this).html(index+1);
		});

	}


	function ajaxFileUploadImg(){
		//获取file的全部id
		var uplist = $("input[id^=uploads]");
		//alert(JSON.stringify(uplist));
		if(uplist.length==0){
			$.ligerDialog.SQalert({content: "没有要上传的内容！" , SQ_type: "warning" });
			return;
		}
		var arrId = [];
		var fileNames = [];//用于判断是否有重名文件
		for (var i=0; i< uplist.length; i++){
			if(uplist[i].value){
				arrId[i] = uplist[i].id;
				if($.inArray(uplist[i].value, fileNames) >= 0){
					$.ligerDialog.SQalert({content: uplist[i].value+"文件重名！" , SQ_type: "warning" });
					return;
				}
				fileNames[i] = uplist[i].value;
			}
		}
		var _filePath = $("#filePath").val();
		var wait_dialog=$.ligerDialog.SQalert({content: '上传中,请稍后...' ,SQ_type: "waitting" });
		$.ajaxFileUpload({
			url:'<%=path%>/system/sysulfilesrec/upload.do',
			secureuri:false,
			fileElementId: arrId,  //这里不在是以前的id了，要写成数组的形式！
			dataType: 'json',
			data: {filePath:_filePath},
			success: function (msg){
				wait_dialog.close();
				if(msg.success == 1){
					$.ligerDialog.SQalert({content:msg.info, SQ_type: "success"});
					//alert(JSON.stringify(msg.data));
					upload_result_params=msg.data;
					uploadCallbackForMore(msg.data);

					//遍历msg.data 为filePath加上服务器路径前缀

					if(serverPrefixPath.length >1 && serverPrefixPath.indexOf("\"")>=0){
						serverPrefixPath = serverPrefixPath.substring(1,serverPrefixPath.length-1);
					}
					var uploadJOSN = JSON.parse(JSON.stringify(msg.data));;
					for (var i=0; i< uploadJOSN.length; i++){
						var oldPath = "";
						var newPath = "";
						oldPath = uploadJOSN[i].filePath;
						newPath = serverPrefixPath + oldPath;
						uploadJOSN[i].filePath = newPath;
					}

					//上传成功后在文件后加入下载按钮
					$('.upfileBoxs [type=file]').each(function(index, el) {
						$(el).afterUploadFile({
							filenameField: 'fileNm', //默认值就是fileNm
							filePathField: 'filePath', //默认值就是filePath
							files_DATA: uploadJOSN
						});
					});
				} else{
					$.ligerDialog.SQalert({content:msg.info, SQ_type: "error"});
				}
			},
			complete: function(){

			}
		});
	}
	//获取上传结果参数
	function getUploadResultParams(){
		return upload_result_params;
	}
	//清除上传结果参数
	function clearUploadResultParams(){
		//移除文件上传标签
		$(".fileUploadBox").remove();
		upload_result_params = [];//设置参数为空
		FileCount = 1;
		$("#add_upfileBox2").click();
	}

	//上传回调方法
	function uploadCallbackForMore(_data){

	}

</script>
